package tables

import (
	"github.com/GoAdminGroup/go-admin/context"
	"github.com/GoAdminGroup/go-admin/modules/db"
	"github.com/GoAdminGroup/go-admin/plugins/admin/modules/table"
	"github.com/GoAdminGroup/go-admin/template/types"
	"github.com/GoAdminGroup/go-admin/template/types/form"
	editType "github.com/GoAdminGroup/go-admin/template/types/table"
)

func GetAyextfieldTable(ctx *context.Context) table.Table {

	ayExtfield := table.NewDefaultTable(ctx, table.DefaultConfigWithDriverAndConnection("sqlite", "pbootcms"))

	info := ayExtfield.GetInfo()
	mopts := optsFromTbl("ay_model", "name", "mcode")
	info.AddField("Id", "id", db.Integer)
	info.AddField("模型编码", "mcode", db.Text).
		FieldEditAble(editType.Select).FieldEditOptions(mopts)
	info.AddField("模型名称", "name1", db.Varchar).
		FieldDisplay(func(value types.FieldModel) any {
			name, _ := value.Row["ay_model_goadmin_join_name"].(string)
			return name
		})
	info.AddField("modelname", "name", db.Varchar).FieldJoin(types.Join{
		Field:     "mcode",
		JoinField: "id",
		Table:     "ay_model",
	}).FieldHide()

	info.AddField("字段名称", "name", db.Text).
		FieldEditAble()
	info.AddField("字段类型", "type", db.Text).FieldEditAble(editType.Select).
		FieldEditOptions(typeOpts)
	info.AddField("单选或多选值", "value", db.Text).
		FieldEditAble()
	info.AddField("描述文本", "description", db.Text).
		FieldEditAble()
	info.AddField("排序", "sorting", db.Integer).
		FieldEditAble()

	info.SetTable("ay_extfield").SetTitle("扩展字段").SetDescription("扩展字段")

	formList := ayExtfield.GetForm()
	formList.AddField("Id", "id", db.Integer, form.Default).
		FieldDisableWhenCreate().FieldDisableWhenUpdate()
	formList.AddField("模型编码", "mcode", db.Text, form.SelectSingle).
		FieldOptionsFromTable("ay_model", "name", "mcode")
	formList.AddField("字段名称", "name", db.Text, form.Text)
	formList.AddField("字段类型", "type", db.Text, form.SelectSingle).
		FieldOptions(typeOpts)
	formList.AddField("单选或多选值", "value", db.Text, form.Text)
	formList.AddField("描述文本", "description", db.Text, form.Text)
	formList.AddField("排序", "sorting", db.Integer, form.Number)

	formList.SetTable("ay_extfield").SetTitle("编辑扩展字段").SetDescription("编辑扩展字段")

	return ayExtfield
}
